package com.letv.app.ljodfh.ui.model;

import android.support.annotation.NonNull;

import com.jiongbull.jlog.JLog;
import com.letv.app.ljodfh.beans.TvLivingList;
import com.letv.app.ljodfh.http.NetResponseObject;
import com.letv.app.ljodfh.http.NetUtils;
import com.letv.app.ljodfh.ui.contract.ChannelContract;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * @author suming
 * @date 2020/4/15 9:31
 */
public class ChannelModel implements ChannelContract.Model {
    @Override
    public void fetchData(@NonNull String url, @NonNull ChannelContract.Presenter presenter) {
        NetUtils.getNetAdapter()
                .fetchChannel(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<NetResponseObject<TvLivingList>>() {
                    @Override
                    protected void onStart() {
                        super.onStart();
                        presenter.onStart();
                    }

                    @Override
                    public void onNext(NetResponseObject<TvLivingList> response) {
                        if (response.isNetOk()) {
                            presenter.getData(response.getData());
                        } else {
                            presenter.onError(response.getError());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        JLog.e(e);
                        presenter.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}
